package com.jinyi.wwdeng.pro

import com.jinyi.base.base.BaseActivity
import com.jinyi.base.network.IRequestCallback
import com.jinyi.base.network.NetworkUtils
import com.jinyi.lib_http.model.HttpParams
import com.jinyi.wwdeng.network.ApiService
import org.json.JSONObject

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/25 18:04
 * @Annotation：
 */
abstract class ProAct: BaseActivity() {

    override fun onRequestFinish() {
    }

    /**
     * 开始请求网络 Get
     * @param url       : 接口地址
     * @param params      : 参数
     * @param callback  :  回调
     */
    open fun onGetRequestAction(url: String, params: HttpParams?, callback: IRequestCallback) {
        NetworkUtils.get(url,params,object : IRequestCallback {
            //请求成功
            override fun onRequestSuccess(data: JSONObject) {
                val code = data.optInt("code")
                when(code){
                    0 -> {//成功
                        callback.onRequestSuccess(data)
                    }
                    else -> {
                        val msg = data.optString("msg")
                        showToast(msg)
                    }
                }
            }
            //请求失败
            override fun onRequestError(data: JSONObject) {
                callback.onRequestError(data)
            }
            //请求完成
            override fun onRequestFinish() {
                callback.onRequestFinish()
                hideLoading()
            }
        })
    }

    /**
     * 开始请求网络 Post
     * @param url       : 接口地址
     * @param params    : 参数 JSONObject
     * @param callback  :  回调
     */
    open fun onPostRequestAction(url: String, params: JSONObject, callback: IRequestCallback) {
        val httpUrl = ApiService.BASE_URL + url
        NetworkUtils.post(httpUrl,params,object : IRequestCallback {
            //请求成功
            override fun onRequestSuccess(data: JSONObject) {
                val code = data.optInt("code")
                when(code){
                    0 -> {//成功
                        callback.onRequestSuccess(data)
                    }
                    else -> {
                        val msg = data.optString("msg")
                        showToast(msg)
                    }
                }
            }
            //请求失败
            override fun onRequestError(data: JSONObject) {
                callback.onRequestError(data)
            }
            //请求完成
            override fun onRequestFinish() {
                callback.onRequestFinish()
                hideLoading()
            }
        })
    }

    /**
     * 开始请求网络 Post
     * @param url       : 接口地址
     * @param params      : 参数
     * @param callback  :  回调
     */
    open fun onPostRequestAction(url: String, params: HttpParams?, callback: IRequestCallback) {
        NetworkUtils.post(url,params,object : IRequestCallback {
            //请求成功
            override fun onRequestSuccess(data: JSONObject) {
                val code = data.optInt("code")
                when(code){
                    0 -> {//成功
                        callback.onRequestSuccess(data)
                    }
                    else -> {
                        val msg = data.optString("msg")
                        showToast(msg)
                    }
                }
            }
            //请求失败
            override fun onRequestError(data: JSONObject) {
                callback.onRequestError(data)
            }
            //请求完成
            override fun onRequestFinish() {
                callback.onRequestFinish()
                hideLoading()
            }
        })
    }
}